package com.zhangzhi.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

public class DateUtil {
	private static SimpleDateFormat sdfYYMM = new SimpleDateFormat("yyMM");
	private static SimpleDateFormat sdfMMDD = new SimpleDateFormat("MMdd");
	private static SimpleDateFormat sdfYYMMDD = new SimpleDateFormat("yyMMdd");
	private static SimpleDateFormat sdfHHMMSS = new SimpleDateFormat("HHmmss");
	private static SimpleDateFormat sdfHMS = new SimpleDateFormat("HH:mm:ss");
	private static SimpleDateFormat sdfMMDDhhmmss = new SimpleDateFormat(
			"MMddHHmmss");
	private static SimpleDateFormat sdfyyMMDDhhmmss = new SimpleDateFormat(
			"yyMMddHHmmss");
	private static SimpleDateFormat y2M2d24 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	private static SimpleDateFormat y2M2dH = new SimpleDateFormat(
			"yyyy-MM-dd HH");

	private static SimpleDateFormat y2M2d = new SimpleDateFormat("yyyy-MM-dd");

	private static SimpleDateFormat y2m = new SimpleDateFormat("yyyy年MM月");

	private static SimpleDateFormat HH = new SimpleDateFormat("HH");

	private static SimpleDateFormat yMd = new SimpleDateFormat("yyyy.MM.dd");

	private static SimpleDateFormat yMdHHmm = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	private static SimpleDateFormat sdfHHmm = new SimpleDateFormat("HH:mm");

	/**
	 * 获得指定格式的时间字符串
	 * 
	 * @param pattern
	 * @return String
	 * @author lizp
	 * @date 2012-12-28
	 */
	public static String getDateTime(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date());
	}

	public static String formatDateMMDDhhmmss(Date date) {
		synchronized (sdfMMDDhhmmss) {
			return sdfMMDDhhmmss.format(date);
		}
	}
	
	public static String formatDateyyMMDDhhmmss(Date date){
		synchronized (sdfyyMMDDhhmmss) {
			return sdfyyMMDDhhmmss.format(date);
		}
	}

	public static String formatDateYYMMDD(Date date) {
		synchronized (sdfYYMMDD) {
			return sdfYYMMDD.format(date);
		}
	}
	
	public static String formatDateYYMM(Date date) {
		synchronized (sdfYYMM) {
			return sdfYYMM.format(date);
		}
	}

	public static String formatDateMMDD(Date date) {
		synchronized (sdfMMDD) {
			return sdfMMDD.format(date);
		}
	}

	public static String formatTimeHMS(Date date) {
		synchronized (sdfHMS) {
			return sdfHMS.format(date);
		}
	}
	
	public static String formatDateHHMMSS(Date date) {
		synchronized (sdfHHMMSS) {
			return sdfHHMMSS.format(date);
		}
	}

	/**
	 * 返回日期格式:yyyy-MM-dd
	 * 
	 * @param date
	 * @return yyyy-MM-dd
	 */
	public static String formatYYMMdd(Date date) {
		synchronized (y2M2d) {
			return y2M2d.format(date);
		}
	}

	public static Integer formatHH(Date date) {
		synchronized (HH) {
			return Integer.parseInt(HH.format(date));
		}
	}

	/**
	 * 转换成字符串
	 * 
	 * @param date
	 * @return 字符串格式为:yyyy-MM-dd HH:mm:ss
	 */
	public static String formatDate(Date date) {
		if (date == null)
			return "";
		synchronized (y2M2d24) {
			return y2M2d24.format(date);
		}
	}

	/**
	 * 转换成日期,字符串格式为 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public synchronized static Date string2Date(String date) {
		Date d = null;
		try {
			d = y2M2d24.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 转换成日期,字符串格式为 yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static Date string2DateYYMMDD(String date) {
		Date d = null;
		try {
			d = y2M2d.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static Date string2DateyMdHHmm(String date) {
		Date d = null;
		try {
			d = yMdHHmm.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 得到minute 分钟后的时间,以yyyy-MM-dd HH:mm:ss 格式的字符串返回
	 * 
	 * @return 以 HH:mm:ss 格式的字符串返回
	 * */
	public static String getAddTimeFormat(int minute) {
		java.util.Date d = new java.util.Date(System.currentTimeMillis()
				+ minute * 60 * 1000);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimeZone zone = new SimpleTimeZone(28800000, "Asia/Shanghai");
		df.setTimeZone(zone);
		return df.format(d);
	}

	/**
	 * 例如：num = -30 oldTime is:2013-02-21 10:12:16 newTme is:2013-01-22 10:12:16
	 * 
	 * @Title: getBeforeMonth
	 * @Description: 获取月份
	 * @param @param num 值为月份取正负
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String getBeforeMonth(int num) {
		Calendar cal = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历。
		cal.add(Calendar.DAY_OF_MONTH, num);// 取当前日期的前一天.
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return format.format(cal.getTime());
	}

	/**
	 * 
	 * @Title: getSysDate
	 * @Description: 以yyyy-MM-dd HH:mm:ss格式的字符串返回当前日期
	 * @param @param date
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public synchronized static String getSysDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date());
	}

	/**
	 * 得到当前时间,以yyyy-MM-dd HH:mm格式的字符串返回
	 * 
	 * @return 以yyyy-MM-dd HH:mm格式的字符串返回当前日期
	 * */
	public static String getStartOfDay() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		return df.format(c.getTime());
	}

	/**
	 * 得到当前时间,以yyyy-MM-dd HH:mm格式的字符串返回
	 * 
	 * @return 以yyyy-MM-dd HH:mm格式的字符串返回当前日期
	 * */
	public static String getEndOfDay() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		return df.format(c.getTime());
	}

	public static String formatYYMM(Date date) {
		synchronized (y2m) {
			return y2m.format(date);
		}
	}

	public static Date formatTs2Date(Timestamp ts) {
		return string2Date(formatDate(new Date(ts.getTime())));
	}

	public static Date StrToDate(String str) {
		Date date = null;
		try {
			date = y2M2d24.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获得本周的第一天，周一
	 * 
	 * @return
	 */
	public Date getCurrentWeekDayStartTime() {
		Calendar c = Calendar.getInstance();
		try {
			int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
			c.add(Calendar.DATE, -weekday);
			c.setTime(y2M2d24.parse(y2M2d.format(c.getTime()) + " 00:00:00"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 获得本周的最后一天，周日
	 * 
	 * @return
	 */
	public Date getCurrentWeekDayEndTime() {
		Calendar c = Calendar.getInstance();
		try {
			int weekday = c.get(Calendar.DAY_OF_WEEK);
			c.add(Calendar.DATE, 8 - weekday);
			c.setTime(y2M2d24.parse(y2M2d.format(c.getTime()) + " 23:59:59"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 获得本天的开始时间，即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public Date getCurrentDayStartTime() {
		Date now = new Date();
		try {
			now = y2M2d.parse(y2M2d.format(now));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本天的结束时间，即2012-01-01 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentDayEndTime() {
		Date now = new Date();
		try {
			now = y2M2d24.parse(y2M2d.format(now) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本小时的开始时间，即2012-01-01 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentHourStartTime() {
		Date now = new Date();
		try {
			now = y2M2dH.parse(y2M2dH.format(now));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本小时的结束时间，即2012-01-01 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentHourEndTime() {
		Date now = new Date();
		try {
			now = y2M2d24.parse(y2M2dH.format(now) + ":59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获得本月的开始时间，即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public Date getCurrentMonthStartTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.DATE, 1);
			now = y2M2d.parse(y2M2d.format(c.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前月的结束时间，即2012-01-31 23:59:59
	 * 
	 * @return
	 */
	public static Date getCurrentMonthEndTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.DATE, 1);
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.DATE, -1);
			now = y2M2d24.parse(y2M2d.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的开始时间，即2012-01-01 00:00:00
	 * 
	 * @return
	 */
	public Date getCurrentYearStartTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.MONTH, 0);
			c.set(Calendar.DATE, 1);
			now = y2M2d.parse(y2M2d.format(c.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前年的结束时间，即2012-12-31 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentYearEndTime() {
		Calendar c = Calendar.getInstance();
		Date now = null;
		try {
			c.set(Calendar.MONTH, 11);
			c.set(Calendar.DATE, 31);
			now = y2M2d24.parse(y2M2d.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前季度的开始时间，即2012-01-1 00:00:00
	 * 
	 * @return
	 */
	public Date getCurrentQuarterStartTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3)
				c.set(Calendar.MONTH, 0);
			else if (currentMonth >= 4 && currentMonth <= 6)
				c.set(Calendar.MONTH, 3);
			else if (currentMonth >= 7 && currentMonth <= 9)
				c.set(Calendar.MONTH, 4);
			else if (currentMonth >= 10 && currentMonth <= 12)
				c.set(Calendar.MONTH, 9);
			c.set(Calendar.DATE, 1);
			now = y2M2d24.parse(y2M2d.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 当前季度的结束时间，即2012-03-31 23:59:59
	 * 
	 * @return
	 */
	public Date getCurrentQuarterEndTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 3) {
				c.set(Calendar.MONTH, 2);
				c.set(Calendar.DATE, 31);
			} else if (currentMonth >= 4 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 5);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 7 && currentMonth <= 9) {
				c.set(Calendar.MONTH, 8);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 10 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 11);
				c.set(Calendar.DATE, 31);
			}
			now = y2M2d24.parse(y2M2d.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获取前/后半年的开始时间
	 * 
	 * @return
	 */
	public Date getHalfYearStartTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 0);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 6);
			}
			c.set(Calendar.DATE, 1);
			now = y2M2d24.parse(y2M2d.format(c.getTime()) + " 00:00:00");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;

	}

	/**
	 * 获取前/后半年的结束时间
	 * 
	 * @return
	 */
	public Date getHalfYearEndTime() {
		Calendar c = Calendar.getInstance();
		int currentMonth = c.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6) {
				c.set(Calendar.MONTH, 5);
				c.set(Calendar.DATE, 30);
			} else if (currentMonth >= 7 && currentMonth <= 12) {
				c.set(Calendar.MONTH, 11);
				c.set(Calendar.DATE, 31);
			}
			now = y2M2d24.parse(y2M2d.format(c.getTime()) + " 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now;
	}

	/**
	 * 获取前一天日期 及星期
	 */
	private void initDate() {
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (i < 0) {
			i = 0;
		}
		String yesterday = new SimpleDateFormat("yyyy年MM月dd日").format(cal
				.getTime());
		String w = weekDays[i];
	}

	public static String timestampToStr(Timestamp ts) {
		return y2M2d24.format(ts);
	}

	public static String timestampToStrYMD(Timestamp ts) {
		return y2M2d.format(ts);
	}

	public static String dateToYYMMDD(Date d) {
		return sdfYYMMDD.format(d);
	}

	public static void main(String[] args) {
		System.out.println(formatDateYYMMDD(new Date()));
	}

	public static String formatDateYMD(Date date) {
		synchronized (yMd) {
			return yMd.format(date);
		}
	}

	/**
	 * HH:mm 格式时间转化成日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date stringHHmm2Date(String date) {
		Date d = null;
		try {
			d = sdfHHmm.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 返回 日期
	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm
	 */
	public static String formatDateyMdHHmm(Date date) {
		if (date == null) {
			return "";
		}
		return yMdHHmm.format(date);
	}

	/**
	 * 格式化date，返回格式为：yyyy-mm
	 * 
	 * @param Timestamp
	 *            ts
	 * @return
	 */
	public static String formatYYAndMM(Timestamp ts) {
		DateFormat df = new SimpleDateFormat("yyyy-MM");
		return df.format(ts);
	}

}